import api from '@/common/request/'
import store from '@/store'

const state = {
    openid: uni.getStorageSync('openid') ?? '',
    mobile: uni.getStorageSync('mobile') ?? '',
    token: uni.getStorageSync('user_token') ?? '',
    headimg: uni.getStorageSync('user_headimg') ?? '',
    share_code: uni.getStorageSync('share_code') ?? '',
    GoEasyUUID: uni.getStorageSync('GoEasyUUID') ?? ''
}

const getters = {
	
}

const actions = {
	mobileLogin({commit}, params) {
		return new Promise((resolve, reject) => {
			api.post('/api/login.mobile_login/mobileLogin', params).then((res)=>{
				if(res.code == 0){
					commit('setMobile', res.data.mobile)
					commit('setToken', res.data.token)
					commit('setHeadimg', res.data.headimg)
					commit('setShareCode', res.data.share_code)
					commit('setGoEasyUUID', res.data.GoEasyUUID)
					
					store.dispatch('GoEasy/connectGoEasy')
					
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	},
	pwdLogin({commit}, params) {
		return new Promise((resolve, reject) => {
			api.post('/api/login.password_login/passwordLogin', params).then((res)=>{
				if(res.code == 0){
					commit('setMobile', res.data.mobile)
					commit('setToken', res.data.token)
					commit('setHeadimg', res.data.headimg)
					commit('setShareCode', res.data.share_code)
					commit('setGoEasyUUID', res.data.GoEasyUUID)
					
					store.dispatch('GoEasy/connectGoEasy')
					
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	},
	retrievePwd({commit}, params) {
		return new Promise((resolve, reject) => {
			api.post('/api/login.retrieve_pwd/retrievePwd', params).then((res)=>{
				if(res.code == 0){
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	},
	wexinOauthLogin({commit}, params) {
		return new Promise((resolve, reject) => {
			api.post('/api/login.weixin_oauth_login/weixinLogin', params).then((res)=>{
				if(res.code == 0){
					commit('setOpenid', res.data.openid)
					commit('setMobile', res.data.mobile)
					commit('setToken', res.data.token)
					commit('setHeadimg', res.data.headimg)
					commit('setShareCode', res.data.share_code)
					commit('setGoEasyUUID', res.data.GoEasyUUID)
					
					store.dispatch('GoEasy/connectGoEasy')
					
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	},
	wexinAppOauthLogin({commit}, params) {
		return new Promise((resolve, reject) => {
			console.log('---------------')
			api.post('/api/login.WeixinOauthLogin/appLogin', params).then((res)=>{
				console.log(res)
				if(res.code == 0){
					commit('setOpenid', res.data.openid)
					commit('setMobile', res.data.mobile)
					commit('setToken', res.data.token)
					commit('setHeadimg', res.data.headimg)
					commit('setShareCode', res.data.share_code)
					commit('setGoEasyUUID', res.data.GoEasyUUID)
					
					store.dispatch('GoEasy/connectGoEasy')
					
					resolve(res.data)
				}else{
					reject()
				}
			})
		});
	},
	loginOut({commit}) {
		commit('setOpenid', '')
		commit('setMobile', '')
		commit('setToken', '')
		commit('setHeadimg', '')
		commit('setShareCode', '')
		commit('setGoEasyUUID', '')
	}
}

const mutations = {
	setOpenid(state, data) {
		state.openid = data
		uni.setStorageSync('openid', data);
	},
    setMobile(state, data) {
    	state.mobile = data
    	uni.setStorageSync('mobile', data);
    },
	setToken(state, data) {
		state.token = data
		uni.setStorageSync('user_token', data);
	},
	setHeadimg(state, data) {
		state.headimg = data
		uni.setStorageSync('user_headimg', data);
	},
	setShareCode(state, data) {
		state.share_code = data
		uni.setStorageSync('share_code', data);
	},
	setGoEasyUUID(state, data) {
		state.GoEasyUUID = data
		uni.setStorageSync('GoEasyUUID', data);
	}
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
